package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.bean.Sale;
import com.lovo.bean.Tenderinfo2;
import com.lovo.dto.OrderDto;
import com.lovo.dto.TenderInfoDto;
import com.lovo.mapper.SaleMapper;
import com.lovo.service.ISaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * <p>
 * ????? 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, Sale> implements ISaleService {

    @Autowired
    SaleMapper saleMapper;


    @Override
    public IPage<Sale> findAllSale(Integer PageNO) {

        return this.getBaseMapper().selectPage(new Page<>(PageNO, 5), null);
    }

    @RabbitListener(queues = "sellQueue")
    public void sendSale(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, OrderDto orderDto) throws IOException {

        Sale sale = new Sale();
        BeanUtils.copyProperties(orderDto, sale);

        saleMapper.insert(sale);
        //手动确认
        channel.basicAck(tag, false);
    }


    @Override
    public IPage<Sale> findAllSaleByUserId(Integer PageNO, Integer userId) {
        QueryWrapper<Sale> qw = new QueryWrapper<>();
        qw.eq("userId", userId);
        return this.getBaseMapper().selectPage(new Page<>(PageNO, 5), qw);
    }


}
